import json, responses, requests, unittest


class responseTest(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    @responses.activate
    def test_case1(self):
        def request_callback(request):
            payload = json.loads(request.body)
            resp_body = {'value': sum(payload['numbers'])}
            headers = {'request-id': '728d329e-0e86-11e4-a748-0c84dc037c13'}
            return (200, headers, json.dumps(resp_body))

        responses.add_callback(
            responses.POST, 'http://calc.com/sum',
            callback=request_callback,
            content_type='application/json',
        )

        resp = requests.post(
            'http://calc.com/sum',
            json.dumps({'numbers': [1, 2, 3]}),
            headers={'content-type': 'application/json'},
        )

        self.assertEqual(resp.json()['value'], 6)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.headers['request-id'], '728d329e-0e86-11e4-a748-0c84dc037c13')


if __name__ == '__main__':
    unittest.main()
